package cloud.seri.auth.web.rest

import cloud.seri.auth.SeriAuthApp

import cloud.seri.auth.config.SecurityBeanOverrideConfiguration

import cloud.seri.auth.domain.Dept
import cloud.seri.auth.repository.DeptRepository
import cloud.seri.auth.service.DeptService
import cloud.seri.auth.service.dto.DeptDTO
import cloud.seri.auth.service.mapper.DeptMapper
import cloud.seri.auth.web.rest.errors.ExceptionTranslator

import kotlin.test.assertNotNull

import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.MockitoAnnotations
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.data.web.PageableHandlerMethodArgumentResolver
import org.springframework.http.MediaType
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter
import org.springframework.test.context.junit4.SpringRunner
import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.setup.MockMvcBuilders
import org.springframework.validation.Validator



import cloud.seri.auth.web.rest.TestUtil.createFormattingConversionService
import org.assertj.core.api.Assertions.assertThat
import org.hamcrest.Matchers.hasItem
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.content
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status

/**
 * Test class for the DeptResource REST controller.
 *
 * @see DeptResource
 */
@RunWith(SpringRunner::class)
@SpringBootTest(classes = [SeriAuthApp::class])
class DeptResourceIT {

    @Autowired
    private lateinit var deptRepository: DeptRepository

    @Autowired
    private lateinit var deptMapper: DeptMapper

    @Autowired
    private lateinit var deptService: DeptService

    @Autowired
    private lateinit var jacksonMessageConverter: MappingJackson2HttpMessageConverter

    @Autowired
    private lateinit var pageableArgumentResolver: PageableHandlerMethodArgumentResolver

    @Autowired
    private lateinit var exceptionTranslator: ExceptionTranslator

    @Autowired
    private lateinit var validator: Validator

    private lateinit var restDeptMockMvc: MockMvc

    private lateinit var dept: Dept

    @Before
    fun setup() {
        MockitoAnnotations.initMocks(this)
        val deptResource = DeptResource(deptService)
        this.restDeptMockMvc = MockMvcBuilders.standaloneSetup(deptResource)
            .setCustomArgumentResolvers(pageableArgumentResolver)
            .setControllerAdvice(exceptionTranslator)
            .setConversionService(createFormattingConversionService())
            .setMessageConverters(jacksonMessageConverter)
            .setValidator(validator).build()
    }

    @Before
    fun initTest() {
        deptRepository.deleteAll()
        dept = createEntity()
    }

    @Test
    fun createDept() {
        val databaseSizeBeforeCreate = deptRepository.findAll().size

        // Create the Dept
        val deptDTO = deptMapper.toDto(dept)
        restDeptMockMvc.perform(
            post("/api/depts")
                .contentType(TestUtil.APPLICATION_JSON_UTF8)
                .content(TestUtil.convertObjectToJsonBytes(deptDTO))
        ).andExpect(status().isCreated)

        // Validate the Dept in the database
        val deptList = deptRepository.findAll()
        assertThat(deptList).hasSize(databaseSizeBeforeCreate + 1)
        val testDept = deptList[deptList.size - 1]
        assertThat(testDept.deptName).isEqualTo(DEFAULT_DEPT_NAME)
        assertThat(testDept.fullName).isEqualTo(DEFAULT_FULL_NAME)
        assertThat(testDept.sort).isEqualTo(DEFAULT_SORT)
        assertThat(testDept.remark).isEqualTo(DEFAULT_REMARK)
        assertThat(testDept.status).isEqualTo(DEFAULT_STATUS)
    }

    @Test
    fun createDeptWithExistingId() {
        val databaseSizeBeforeCreate = deptRepository.findAll().size

        // Create the Dept with an existing ID
        dept.id = "existing_id"
        val deptDTO = deptMapper.toDto(dept)

        // An entity with an existing ID cannot be created, so this API call must fail
        restDeptMockMvc.perform(
            post("/api/depts")
                .contentType(TestUtil.APPLICATION_JSON_UTF8)
                .content(TestUtil.convertObjectToJsonBytes(deptDTO))
        ).andExpect(status().isBadRequest)

        // Validate the Dept in the database
        val deptList = deptRepository.findAll()
        assertThat(deptList).hasSize(databaseSizeBeforeCreate)
    }


    @Test
    fun checkDeptNameIsRequired() {
        val databaseSizeBeforeTest = deptRepository.findAll().size
        // set the field null
        dept.deptName = null

        // Create the Dept, which fails.
        val deptDTO = deptMapper.toDto(dept)

        restDeptMockMvc.perform(
            post("/api/depts")
                .contentType(TestUtil.APPLICATION_JSON_UTF8)
                .content(TestUtil.convertObjectToJsonBytes(deptDTO))
        ).andExpect(status().isBadRequest)

        val deptList = deptRepository.findAll()
        assertThat(deptList).hasSize(databaseSizeBeforeTest)
    }

    @Test
    fun getAllDepts() {
        // Initialize the database
        deptRepository.save(dept)

        // Get all the deptList
        restDeptMockMvc.perform(get("/api/depts?sort=id,desc"))
            .andExpect(status().isOk)
            .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
            .andExpect(jsonPath("$.[*].id").value(hasItem(dept.id)))
            .andExpect(jsonPath("$.[*].deptName").value(hasItem(DEFAULT_DEPT_NAME)))
            .andExpect(jsonPath("$.[*].fullName").value(hasItem(DEFAULT_FULL_NAME)))
            .andExpect(jsonPath("$.[*].sort").value(hasItem(DEFAULT_SORT)))
            .andExpect(jsonPath("$.[*].remark").value(hasItem(DEFAULT_REMARK)))
            .andExpect(jsonPath("$.[*].status").value(hasItem(DEFAULT_STATUS)))
    }
    
    @Test
    fun getDept() {
        // Initialize the database
        deptRepository.save(dept)

        val id = dept.id
        assertNotNull(id)

        // Get the dept
        restDeptMockMvc.perform(get("/api/depts/{id}", id))
            .andExpect(status().isOk)
            .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
            .andExpect(jsonPath("$.id").value(id))
            .andExpect(jsonPath("$.deptName").value(DEFAULT_DEPT_NAME))
            .andExpect(jsonPath("$.fullName").value(DEFAULT_FULL_NAME))
            .andExpect(jsonPath("$.sort").value(DEFAULT_SORT))
            .andExpect(jsonPath("$.remark").value(DEFAULT_REMARK))
            .andExpect(jsonPath("$.status").value(DEFAULT_STATUS))
    }

    @Test
    fun getNonExistingDept() {
        // Get the dept
        restDeptMockMvc.perform(get("/api/depts/{id}", Long.MAX_VALUE))
            .andExpect(status().isNotFound)
    }

    @Test
    fun updateDept() {
        // Initialize the database
        deptRepository.save(dept)

        val databaseSizeBeforeUpdate = deptRepository.findAll().size

        // Update the dept
        val id = dept.id
        assertNotNull(id)
        val updatedDept = deptRepository.findById(id).get()
        updatedDept.deptName = UPDATED_DEPT_NAME
        updatedDept.fullName = UPDATED_FULL_NAME
        updatedDept.sort = UPDATED_SORT
        updatedDept.remark = UPDATED_REMARK
        updatedDept.status = UPDATED_STATUS
        val deptDTO = deptMapper.toDto(updatedDept)

        restDeptMockMvc.perform(
            put("/api/depts")
                .contentType(TestUtil.APPLICATION_JSON_UTF8)
                .content(TestUtil.convertObjectToJsonBytes(deptDTO))
        ).andExpect(status().isOk)

        // Validate the Dept in the database
        val deptList = deptRepository.findAll()
        assertThat(deptList).hasSize(databaseSizeBeforeUpdate)
        val testDept = deptList[deptList.size - 1]
        assertThat(testDept.deptName).isEqualTo(UPDATED_DEPT_NAME)
        assertThat(testDept.fullName).isEqualTo(UPDATED_FULL_NAME)
        assertThat(testDept.sort).isEqualTo(UPDATED_SORT)
        assertThat(testDept.remark).isEqualTo(UPDATED_REMARK)
        assertThat(testDept.status).isEqualTo(UPDATED_STATUS)
    }

    @Test
    fun updateNonExistingDept() {
        val databaseSizeBeforeUpdate = deptRepository.findAll().size

        // Create the Dept
        val deptDTO = deptMapper.toDto(dept)

        // If the entity doesn't have an ID, it will throw BadRequestAlertException
        restDeptMockMvc.perform(
            put("/api/depts")
                .contentType(TestUtil.APPLICATION_JSON_UTF8)
                .content(TestUtil.convertObjectToJsonBytes(deptDTO))
        ).andExpect(status().isBadRequest)

        // Validate the Dept in the database
        val deptList = deptRepository.findAll()
        assertThat(deptList).hasSize(databaseSizeBeforeUpdate)
    }

    @Test
    fun deleteDept() {
        // Initialize the database
        deptRepository.save(dept)

        val databaseSizeBeforeDelete = deptRepository.findAll().size

        val id = dept.id
        assertNotNull(id)

        // Delete the dept
        restDeptMockMvc.perform(
            delete("/api/depts/{id}", id)
                .accept(TestUtil.APPLICATION_JSON_UTF8)
        ).andExpect(status().isNoContent)

        // Validate the database is empty
        val deptList = deptRepository.findAll()
        assertThat(deptList).hasSize(databaseSizeBeforeDelete - 1)
    }

    @Test
    fun equalsVerifier() {
        TestUtil.equalsVerifier(Dept::class.java)
        val dept1 = Dept()
        dept1.id = "id1"
        val dept2 = Dept()
        dept2.id = dept1.id
        assertThat(dept1).isEqualTo(dept2)
        dept2.id = "id2"
        assertThat(dept1).isNotEqualTo(dept2)
        dept1.id = null
        assertThat(dept1).isNotEqualTo(dept2)
    }

    @Test
    fun dtoEqualsVerifier() {
        TestUtil.equalsVerifier(DeptDTO::class.java)
        val deptDTO1 = DeptDTO()
        deptDTO1.id = "id1"
        val deptDTO2 = DeptDTO()
        assertThat(deptDTO1).isNotEqualTo(deptDTO2)
        deptDTO2.id = deptDTO1.id
        assertThat(deptDTO1).isEqualTo(deptDTO2)
        deptDTO2.id = "id2"
        assertThat(deptDTO1).isNotEqualTo(deptDTO2)
        deptDTO1.id = null
        assertThat(deptDTO1).isNotEqualTo(deptDTO2)
    }

    companion object {

        private const val DEFAULT_DEPT_NAME: String = "AAAAAAAAAA"
        private const val UPDATED_DEPT_NAME = "BBBBBBBBBB"

        private const val DEFAULT_FULL_NAME: String = "AAAAAAAAAA"
        private const val UPDATED_FULL_NAME = "BBBBBBBBBB"

        private const val DEFAULT_SORT: Int = 1
        private const val UPDATED_SORT: Int = 2

        private const val DEFAULT_REMARK: String = "AAAAAAAAAA"
        private const val UPDATED_REMARK = "BBBBBBBBBB"

        private const val DEFAULT_STATUS: Int = 1
        private const val UPDATED_STATUS: Int = 2

        /**
         * Create an entity for this test.
         *
         * This is a static method, as tests for other entities might also need it,
         * if they test an entity which requires the current entity.
         */
        @JvmStatic
        fun createEntity(): Dept {
            val dept = Dept(
                deptName = DEFAULT_DEPT_NAME,
                fullName = DEFAULT_FULL_NAME,
                sort = DEFAULT_SORT,
                remark = DEFAULT_REMARK,
                status = DEFAULT_STATUS
            )
            return dept
        }
    }
}
